package com.nondo.dean.platform.controller.api.inner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Attach;
import com.nondo.dean.platform.service.AttachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/4/8
 */
@RestController("apiInnerAttach")
@RequestMapping("/{v}/inner/attach")
public class AttachController {

    @Autowired
    AttachService attachService;

    @ApiVersion(1)
    @RequestMapping("/list")
    public View<List> list(HttpServletRequest request, HttpServletResponse response, @RequestBody String result) {
        View<List> view = new View<List>(20000, "获取数据成功");

        Object jsonObject = JSON.parse(result);
        String jsonString = jsonObject.toString();
        List listId = JSONObject.parseArray(jsonString);
        List list = this.attachService.getList(listId);

        view.setData(list);
        return view;
    }

    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<Attach> info(HttpServletRequest request, HttpServletResponse response, @PathVariable int id) {
        View<Attach> view = new View<Attach>(20000, "获取数据成功");

        Attach attach = this.attachService.getById(id);

        view.setData(attach);

        return view;
    }

    @ApiVersion(1)
    @RequestMapping("/insert")
    public View<Attach> insert(HttpServletRequest request, HttpServletResponse response, @RequestBody String path) {
        View<Attach> view = new View<Attach>(20000, "插入成功");

        Attach attach = this.attachService.insert(path.replaceAll("\"", ""));

        view.setData(attach);

        return view;
    }
}
